//
//  WbStatusesOriginalView.swift
//  Weibo
//
//  Created by HH on 16/12/5.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit
import SnapKit

class WbStatusesOriginalView: WbBaseView {
    
    var bottomConstraint:Constraint?
    
    var pictureViewModel:WbPictureViewModel = WbPictureViewModel.sharedPictureViewModel
    
//    var topConstraint:Constraint?
    
    
    var statusesViewModel:WbStatusesViewModel?{
        
        didSet{
            
            //传值给ViewModel处理数据
            pictureViewModel.pic_Url = statusesViewModel?.statuses?.pic_urls
            //传值给View展示数据
            pictureView.pic_url = statusesViewModel?.statuses?.pic_urls
            
            let user = statusesViewModel?.statuses?.user
            
            
            if let imageUrl = user?.profile_image_url {
                iconView.sd_setImage(with: URL(string:imageUrl), placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
            }
            //昵称
            nameLabel.text = user?.screen_name
            //来源
            sourceLabel.text = statusesViewModel?.sourceName
            //内容
            contentLabel.text = statusesViewModel?.statuses?.text
            //认证
            verifiedView.image = statusesViewModel?.verified_typeImage
            //会员等级
            memberShipView.image = statusesViewModel?.mbrankImage
            //时间
            timeLabel.text = statusesViewModel?.statusesTime
            
//            print("\(statusesViewModel?.statuses?.user?.verified_type)" + (statusesViewModel?.statuses?.user?.screen_name)!)
            
//            print(statusesViewModel?.statuses!)
           

            bottomConstraint?.deactivate()
            
            if let picUrl = statusesViewModel?.statuses?.pic_urls,picUrl.count > 0 {
                
                pictureView.isHidden = false
                
                self.snp.makeConstraints({ (make) in
                  bottomConstraint =  make.bottom.equalTo(pictureView).offset(marign).constraint
                })
                
//                pictureView.snp.updateConstraints({ (make) in
//                   make.size.equalTo(pictureViewModel.pictureViewSize!)
//                    
//                })
                
            }else{
                
                pictureView.isHidden = true
                
                self.snp.makeConstraints({ (make) in
                    bottomConstraint = make.bottom.equalTo(contentLabel).offset(marign).constraint
                })
                
//               pictureView.snp.updateConstraints({ (make) in
//                   make.size.equalTo(CGSize.zero)
//                })

            }
            
            //不知道为什么,加上去就好用了
//            self.layoutIfNeeded()
        }
        
    }
    
    //图像
    lazy private var iconView = UIImageView.init()
    //会员级别
    lazy private var memberShipView = UIImageView(image: #imageLiteral(resourceName: "common_icon_membership"))
    //昵称
    lazy private var nameLabel = UILabel(text: "", font: 13, textColor: UIColor.darkGray)
    //时间
    lazy private var timeLabel = UILabel(text: "刚刚", font: 13, textColor: UIColor.orange)
    //来源
    lazy private var sourceLabel = UILabel(text: "", font: 12, textColor: UIColor.lightGray)
    //认证
    lazy private var verifiedView = UIImageView(image: #imageLiteral(resourceName: "avatar_enterprise_vip"))
    //内容
    lazy private var contentLabel:UILabel = {
       
        let label = UILabel(text: "", font: 14, textColor: UIColor.darkGray)
        
        label.numberOfLines = 0
        
        return label
        
    }()
    
    //原创微博配图
    lazy private var pictureView:WbStatusesPictureView = {
        
       
        let collectionView = WbStatusesPictureView()
        
        return collectionView
        
    }()
    
    override func setupUI(){
        
        backgroundColor = UIColor.white
        
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberShipView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //切圆
        iconView.layer.cornerRadius = 17.5
        iconView.clipsToBounds = true
        
//        verifiedView.layer
        
        //图像
        iconView.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(marign)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        //昵称
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp.right).offset(marign)
        }
        
        //会员
        memberShipView.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.right).offset(marign)
            make.lastBaseline.equalTo(nameLabel)
        }
        
        //时间
        timeLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(iconView)
            make.left.equalTo(nameLabel)
        }
        
        //来源
        sourceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp.right).offset(marign)
            make.centerY.equalTo(timeLabel)
        }
        
        //认证
        verifiedView.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp.right).offset(-3)
            make.centerY.equalTo(iconView.snp.bottom).offset(-3)
        }
        
        //内容
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp
            .bottom).offset(marign)
            make.left.equalToSuperview().offset(marign)
            make.right.equalToSuperview().offset(-marign)
        }
        
        //原创微博配图
        pictureView.snp.makeConstraints { (make) in
           make.top.equalTo(contentLabel.snp.bottom).offset(marign)
            make.left.equalTo(contentLabel)
            make.size.equalTo(CGSize.zero)
        }
        
        self.snp.makeConstraints { (make) in
        bottomConstraint = make.bottom.equalTo(pictureView).offset(marign).constraint
        }
    }

}


extension WbStatusesOriginalView{
    
    private func test(){
        
        
                    if statusesViewModel?.statuses?.user?.verified_type == 0{
//                        print(statusesViewModel?.statuses)
                    }
                    if let name = statusesViewModel?.statuses?.user?.screen_name {
                        if name == "邻城酒肆" || name == "热壁纸集" || name == "最神奇的视频"{
//                            print(statusesViewModel?.statuses)
        
        
                        }
                    }
        
//                    print("+++++++++")
        
        
//                    print("\(statusesViewModel?.statuses?.user?.verified_type)" + (statusesViewModel?.statuses?.user?.screen_name)!)
//        
//                    print(statusesViewModel?.statuses)
    }
}
